package com.ymatou.iapi.productcategory.testcase;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Ignore;
import org.junit.Test;
import org.junit.experimental.categories.Category;

import com.ymatou.iapi.userservice.parameter.req.CreateUserReq;
import com.ymt.core.tool.FormatUtil;
import com.ymt.core.tool.Logger;
import com.ymt.utils.annotation.*;
import com.ymt.utils.tag.*;
import com.ymttest.database.annotation.SqlMapperFactory;
import com.ymttest.database.annotation.product.IntergratedProductMapper;
import com.ymttest.database.mongo.BuyerProductWapper;
import com.ymatou.iapi.productcategory.parameter.*;
import com.ymatou.iapi.productcategory.service.*;

/**
 * FreeMaker Template自动生成代码
 */
@TestSuite
@Ignore
public class Ts_SaveProductCategory {
	private static SaveProductCategoryBean SaveProductCategoryBean;
	private static SaveProductCategoryCall SaveProductCategoryCall;
	IntergratedProductMapper ipm;
	static BuyerProductWapper BuyerProductWapper;

	@BeforeClass
	public static void setUp() throws NumberFormatException, Exception {
		Logger.createResultFile("添加类目");
		BuyerProductWapper=new BuyerProductWapper();
	}

	@Before
	public void caseUp() {
		SaveProductCategoryBean = new SaveProductCategoryBean();
		SaveProductCategoryCall = new SaveProductCategoryCall();
		try {
			
			ipm= SqlMapperFactory
					.getIntergratedProductMapper();
		} catch (NumberFormatException e) {
			e.printStackTrace();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	@AfterClass
	public static void tearDown() {
		Logger.generateResult(SaveProductCategoryCall.getOpurl());
		BuyerProductWapper.close();
	}

	@After
	public void caseDown() {
		Logger.end();
	}

	@Test
	@Category({ P1.class })
	@TestCase
	public void Tc_SaveProductCategory_001() {
		Logger.start(true, "添加类目成功");
		try {
			List<ProductCategoryDto2> productCategoryList = new ArrayList<ProductCategoryDto2>();
			ProductCategoryDto2 productCategory=initProductCategoryDto();
			//类目不能重复 删除了再加
			cleanData(productCategory.getCategoryId());
			productCategoryList.add(productCategory);
			SaveProductCategoryBean.setProductCategoryList(productCategoryList);
			SaveProductCategoryCall.setData(SaveProductCategoryBean);
			SaveProductCategoryCall.callService();
			Logger.verifyEquals("200", SaveProductCategoryCall.getString("Code"), "验证Code");
			verifyDB(SaveProductCategoryBean);
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	/**
	 * 清理类目
	 * @param cid
	 * @throws Exception
	 */
	private void cleanData(Integer cid) throws Exception {
		if(cid==null) {
			return;
		}
		BuyerProductWapper.deleteProductCatalogInfoByCid(cid);
		ipm.deleteYmtProductCategory(cid);
	}
	/**
	 * 初始化类目
	 * @return
	 */
	private ProductCategoryDto2 initProductCategoryDto() {
		ProductCategoryDto2 ProductCategoryDto2=new ProductCategoryDto2();
		ProductCategoryDto2.setbIsAvailable(true);
		ProductCategoryDto2.setbIsChildNodeId(true);
		ProductCategoryDto2.setCategoryEnName("CategoryEnName");
		ProductCategoryDto2.setCategoryId(10088);
		ProductCategoryDto2.setCategoryName("类目名");
		ProductCategoryDto2.setHighRisk(true);
		ProductCategoryDto2.setLevel(1);
		ProductCategoryDto2.setMasterCategory(0);
		ProductCategoryDto2.setOpeator("test");
		//url(//s2.ymatou.com/home/4aa0780b5ffeee46a01f132052fdb71a.png)
		ProductCategoryDto2.setSamplePic("http://s2.ymatou.com/home/4aa0780b5ffeee46a01f132052fdb71a.png");
		ProductCategoryDto2.setSort(1);
		ProductCategoryDto2.setType(1);
		return ProductCategoryDto2;
	}

	private void verifyDB(SaveProductCategoryBean saveProductCategoryBean2) {
		
		saveProductCategoryBean2.getProductCategoryList().forEach(pc->{
			//验证数据库 Ymt_ProductCategory
			Map<String, Object> m = ipm.selectYmtProductCategory(pc.getCategoryId()).get(0);
			Logger.debug("sql:"+m);
			Logger.verifyEquals(pc.getOpeator(), m.get("sOpeator"), "验证sOpeator");
			Logger.verifyEquals(pc.getSamplePic(), m.get("SamplePic"), "验证SamplePic");
			Logger.verifyEquals(pc.getCategoryName(), m.get("sCategory"), "验证sCategory");
			Logger.verifyEquals(pc.getSort(), m.get("sort"), "验证sort");
			Logger.verifyEquals(pc.getbIsChildNodeId(), m.get("bIsChildNodeId"), "验证bIsChildNodeId");
			Logger.verifyEquals(pc.getCategoryEnName(), m.get("sCategoryEn"), "验证sCategoryEn");
			Logger.verifyEquals(pc.getLevel(), m.get("iLevel"), "验证iLevel");
			Logger.verifyEquals(pc.getMasterCategory(), m.get("iMasterCategory"), "验证iMasterCategory");
			Logger.verifyEquals(pc.getbIsAvailable(), m.get("bIsAvailable"), "验证bIsAvailable");
			Logger.verifyEquals(pc.getHighRisk(), m.get("HighRisk"), "验证HighRisk");
			// {iTag=0, iCategoryId=10088, sOpeator=test, SamplePic=http://s2.ymatou.com/home/4aa0780b5ffeee46a01f132052fdb71a.png, 
			//sCategory=类目名, sort=1, bIsChildNodeId=true, iType=1, sCategoryEn=CategoryEnName, iLevel=1, iOldCategoryId=null, 
			//sCoreWords=null, iMasterCategory=0, dAddTime=null, bIsAvailable=true, HighRisk=true, sMark=null, DataVersion=[B@163d04ff, iAction=0, MeasurePic=null}
			//验证mongo数据库
			try {
				Map mongo = BuyerProductWapper.getProductCatalogInfoByCid(pc.getCategoryId().toString()).get(0);
				Logger.debug("mongo:"+mongo);
				//mongo:{_id=5ebb906a2aed4646431bdafe, MasterCategoryId=0, CategoryId=10088, CategoryName=类目名, CategorySort=1, Level=1, CategoryEnName=CategoryEnName, ProductsNum=0, Action=0, highRisk=true}
				Logger.verifyEquals(pc.getMasterCategory(), mongo.get("MasterCategoryId"), "mongo验证MasterCategoryId");
				Logger.verifyEquals(pc.getCategoryName(), mongo.get("CategoryName"), "mongo验证CategoryName");
				Logger.verifyEquals(pc.getSort(), mongo.get("CategorySort"), "mongo验证CategorySort");
				Logger.verifyEquals(pc.getLevel(), mongo.get("Level"), "mongo验证Level");
				Logger.verifyEquals(pc.getCategoryEnName(), mongo.get("CategoryEnName"), "mongo验证CategoryEnName");
				Logger.verifyEquals(pc.getHighRisk(), mongo.get("highRisk"), "mongo验证highRisk");
			} catch (Exception e) {
				e.printStackTrace();
			}
		});
		
	}
}